<template>
  <!-- 主播管理 -->
  <a-spin class="anchor-review" tip="加载中..." :spinning="isLoading">
    <!-- 筛选 -->
    <div class="filter">
      <div class="l search-box">
        <div>
          <span>POI通过状态：</span>
          <a-select style="width:160px;" placeholder="请选择状态" v-model="search.materialStatus" @change="handleByQueryCond">
            <a-select-option v-for="item in qualificationReviewStatus" :key="item.value" :value="item.value">{{ item.name }}</a-select-option>
          </a-select>
        </div>
        <div>
          <span>产品绑定关系：</span>
          <a-select style="width:160px;" placeholder="请选择绑定关系" v-model="search.bindRelation" @change="handleByQueryCond">
            <a-select-option v-for="item in productBind" :key="item.value" :value="item.value">{{ item.name }}</a-select-option>
          </a-select>
        </div>
      </div>
      <div class="r" style="width:300px;justify-content: flex-end;display:flex">
        <a-button type="primary" @click="batchDelete" style="background:#828485;border-color:#828485;">批量删除</a-button>
        <a-button type="primary" v-if="judgeId === '1'" @click="batchSubmission" style="background:#1d6fe9;margin-left:10px;">批量提交商家资质审核</a-button>
        <a-button type="primary" @click="handleOpenBind" style="background:#34C025;border-color:#34C025;margin-left:10px;">新增绑定</a-button>
        <a-button type="danger" @click="toSpu" style="margin-left:10px;">返回sup列表</a-button>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <AddSpuModal @close="handleCloseModal" @updateList="handleUpdateList" :visible="visible" />

    <!-- 表格 -->
    <div class="main-body">
      <a-table :row-selection="rowSelection" :columns="tableColumns" :pagination="false" :data-source="poiList" :scroll="{ x: 900, y: tableHeight }" rowKey="id">
        <!-- 高德POI信息 -->
        <template slot="poiName" slot-scope="text, record">
          <p><span class="font-weight-bold">门店名称：</span>{{ record.poiName || '' }}</p>
          <p><span class="font-weight-bold">所属城市：</span>{{ record.city || '' }}</p>
          <p><span class="font-weight-bold">门店地址：</span>{{ record.address || '' }}</p>
          <p><span class="font-weight-bold">经度：</span>{{ record.longitude || '' }}</p>
          <p><span class="font-weight-bold">纬度：</span>{{ record.latitude || '' }}</p>
          <p v-if="record.gdId">
            <span class="font-weight-bold">高德POI_ID：</span><a :href="record.gdUrl || ''" target="_blank" v-if="record.gdUrl">{{ record.gdId || '' }}</a
            ><span v-else>{{ record.gdId || '' }}</span>
          </p>
        </template>
        <!-- 抖音POI信息 -->
        <template slot="bytePoiName" slot-scope="text, record">
          <p><span class="font-weight-bold">门店名称：</span>{{ record.bytePoiName || '' }}</p>
          <p><span class="font-weight-bold">门店类型：</span>{{ record.bytePoiTypeName || '' }}</p>
          <p><span class="font-weight-bold">所属城市：</span>{{ record.byteCity || '' }}</p>
          <p><span class="font-weight-bold">门店地址：</span>{{ record.byteAddress || '' }}</p>
          <p><span class="font-weight-bold">经度：</span>{{ record.byteLongitude || '' }}</p>
          <p><span class="font-weight-bold">纬度：</span>{{ record.byteLatitude || '' }}</p>
          <p v-if="record.poiId">
            <span class="font-weight-bold">抖音POI_ID：</span><a :href="record.bytePoiUrl || ''" target="_blank" v-if="record.bytePoiUrl">{{ record.poiId || '' }}</a
            ><span v-else>{{ record.poiId || '' }}</span>
          </p>
        </template>
        <!-- POI融合信息 -->
        <template slot="createTime" slot-scope="text, record">
          <p><span class="font-weight-bold">POI匹配申请时间：</span>{{ record.createTime || '' }}</p>
          <p><span class="font-weight-bold">POI匹配状态：</span>{{ record.matchStatusStr || '' }}</p>
          <p><span class="font-weight-bold">POI同步状态：</span>{{ record.isSyncStr || '' }}</p>
          <p><span class="font-weight-bold">备注：</span>{{ record.matchStatusDesc || '' }}</p>
        </template>
        <!-- 商家资质图片 -->
        <template slot="urls" slot-scope="text, record">
          <div class="zzimg" style="display:flex" v-for="(item, index) in record.qualification" :key="index">
            <p v-for="(v, i) in urlName" :key="i" v-show="index === v.value" style="width:80px;text-align:right;">{{ v.name }} :&nbsp;</p>
            <div v-if="item && item.length > 0">
              <upload v-show="item && item.length > 0" :limit="item.length" disabled :value="item"> </upload>
            </div>
            <div v-else style="margin:0 0 3px 0;">无</div>
          </div>
        </template>
        <!-- 商家资质审核信息 -->
        <template slot="materialStatus" slot-scope="text, record">
          <div>
            <span class="font-weight-bold">商家资质审核状态：</span>
            <p v-for="(item, index) in materialStatus" :key="index">
              <span v-if="record.materialStatus === item.value">{{ item.name }}</span>
            </p>
          </div>
          <p><span class="font-weight-bold">商家资质提审时间：</span>{{ record.materialApplyTime || '' }}</p>
          <p><span class="font-weight-bold">过期时间：</span>{{ record.expirTime || '' }}</p>
          <div>
            <span class="font-weight-bold">资质审核拒绝描述：</span>
            <p v-for="(item, index) in record.materialList" :key="index">{{ index + 1 }} 、 {{ item }}</p>
          </div>
        </template>
        <template slot="action" slot-scope="text, record">
          <div v-if="judgeId === '1'">
            <a-button type="link" v-if="record.bindRelation === 0" @click="bindingDeal(record.id, 1)">绑定</a-button>
            <a-button type="link" v-if="record.bindRelation === 1" @click="bindingDeal(record.id, 0)">解绑</a-button>
            <a-button type="link" v-if="record.materialStatus === 0 || record.materialStatus === 2 || record.materialStatus === 3" @click="syncSJZZ(record.id)">提交商家资质审核</a-button>
            <a-button type="link" v-if="record.materialStatus === 3" @click="refuseDeal(record.id)">拒绝</a-button>
          </div>
          <div v-else>
            <a-button type="link" v-if="record.bindRelation === 0" @click="bindingDeal(record.id, 1)">绑定</a-button>
            <a-button type="link" v-if="record.bindRelation === 1" @click="bindingDeal(record.id, 0)">解绑</a-button>
          </div>
        </template>
      </a-table>
    </div>

    <!-- 分页 -->
    <div class="paging-container">
      <a-pagination
        :pageSize="pagination.pageSize"
        v-model="pagination.pageIndex"
        :total="pagination.total"
        showQuickJumper
        showLessItems
        :showTotal="(total) => `共 ${total} 条`"
        style="margin: 10px auto;"
        @change="handlePageChange"
      />
    </div>
    <a-modal v-model="rejectVisible" title="拒绝审核" @cancel="handleRejectCancel" @ok="handleReject">
      <a-input placeholder="请输入拒绝原因" v-model="rejectInfo" />
    </a-modal>
  </a-spin>
</template>

<script>
import { poiProductListUrl, spuBindUpdateUrl, syncMerchantQualificationReview, rejectAudit, bindingDelete } from '@/service'
import upload from '@/components/upload/upload'
import moment from 'moment'
import { columnsConfig } from '../columns'
import AddSpuModal from './addSpu/index.vue'

export default {
  name: 'poiList',
  components: {
    upload,
    AddSpuModal
  },

  data() {
    return {
      moment,
      // 新增弹窗
      visible: false,
      judgeId: '',
      // loading是否显示
      isLoading: false,
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      },
      // table数据查询条件
      search: {
        bizProductId: '',
        materialStatus: '',
        bindRelation: 1
      },
      // poi列表
      poiList: [],
      // 列表总数据条数

      productId: '',
      urlName: [
        { name: '营业执照', value: 'bizLicencePath' },
        { name: '行业许可证', value: 'qualMaterial' },
        { name: '商家授权函', value: 'contractPath' },
        { name: '商家证明', value: 'provePath' },
        { name: '其他资质', value: 'additionalQual' }
      ],
      selectedRowKeys: [],
      rejectVisible: false,
      rejectId: '',
      rejectInfo: ''
    }
  },

  computed: {
    rowSelection() {
      const { selectedRowKeys } = this
      return {
        selectedRowKeys,
        onChange: this.onSelectChange,
        getCheckboxProps: (record) => ({
          props: {
            // disabled: record.materialStatus === 1 || record.materialStatus === 4 || record.materialStatus === 5, // Column configuration not to be checked
            name: record.poiName
          }
        })
      }
    },
    // 状态
    orderStatus() {
      return [
        {
          name: '全部',
          value: ''
        },
        {
          name: '已预约',
          value: 1
        },
        {
          name: '待使用',
          value: 2
        },
        {
          name: '已完成',
          value: 3
        },
        {
          name: '退款中',
          value: 4
        },
        {
          name: '已退款',
          value: 5
        }
      ]
    },
    materialStatus() {
      return [
        {
          name: '全部',
          value: ''
        },
        {
          name: '审核中',
          value: 0
        },
        {
          name: '审核通过',
          value: 1
        },
        {
          name: '审核拒绝',
          value: 2
        },
        {
          name: '未提交',
          value: 3
        },
        {
          name: '无资质',
          value: 4
        },
        {
          name: '协议已过期',
          value: 5
        }
      ]
    },
    tableColumns() {
      let getColumns = columnsConfig.poiColumnsForSpu || []
      return getColumns
    },
    qualificationReviewStatus() {
      return [
        {
          name: '全部',
          value: ''
        },
        {
          name: '审核中',
          value: 0
        },
        {
          name: '审核通过',
          value: 1
        },
        {
          name: '审核拒绝',
          value: 2
        },
        {
          name: '待提审',
          value: 3
        },
        {
          name: '无资质',
          value: 4
        },
        {
          name: '协议已过期',
          value: 5
        }
      ]
    },
    productBind() {
      return [
        {
          name: '全部',
          value: ''
        },
        {
          name: '绑定',
          value: 1
        },
        {
          name: '解绑',
          value: 0
        }
      ]
    }
  },

  methods: {
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    toSpu() {
      this.$router.go(-1)
    },
    // 开启绑定弹窗
    handleOpenBind() {
      this.visible = true
    },
    // 关闭蒙层
    handleCloseModal() {
      this.visible = false
    },
    // 绑定成功回调
    handleUpdateList() {
      this.visible = false
      this.query()
    },
    /**
     * 通过条件查询
     * @returns {void}
     */
    handleByQueryCond() {
      this.pagination.pageIndex = 1
      this.query()
    },

    /**
     * 分页
     * @param {number} num 当前页码
     * @returns {void}
     */
    handlePageChange(num) {
      this.pagination.pageIndex = num
      this.query()
    },

    // 拿参数
    getParams() {
      const { pageIndex, pageSize } = this.pagination
      const { bizProductId = '', materialStatus, bindRelation } = this.search

      return {
        pageIndex,
        pageSize,
        bizProductId,
        materialStatus,
        bindRelation
      }
    },
    // 设置搜索
    setSearch() {
      const { pageIndex, id, materialStatus, bindRelation } = this.$route.query
      const queryLen = Object.keys(this.$route.query).length
      if (queryLen) {
        this.search = {
          ...this.search,
          pageIndex: pageIndex ? Number(pageIndex) : 1,
          bizProductId: id,
          materialStatus: +materialStatus || '',
          bindRelation: +bindRelation || 1
        }
      }
    },
    // 初始化
    initQuery() {
      this.pagination.pageIndex = 1
      this.pagination.pageSize = 10
      this.setPath(this.getParams())
      this.query()
    },

    /**
     * 获取主播信息列表
     * 具体参数参考 getAnchorInfoListApi 方法
     * @returns {Promise<void>}
     */
    query() {
      this.isLoading = true
      poiProductListUrl(this.getParams())
        .then((rs) => {
          console.log(rs)
          this.poiList = Object.freeze(rs.poiList) || []
          this.pagination.total = rs.total || 0
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 储存搜索字段
    setPath(origin) {
      const { path } = this.$route
      const query = {}
      for (let key in origin) {
        if (origin[key]) query[key] = origin[key]
      }
      if (query.pageIndex <= 1) delete query.pageIndex
      delete query.pageSize
      this.$router
        .replace({
          path,
          query: {
            ...query
          }
        })
        .catch(() => {})
    },
    addBinding() {
      this.$router.push({
        path: '/addSpu',
        query: {
          id: this.productId
        }
      })
    },
    // 解绑1，绑定0操作
    bindingDeal(id = '', type = '') {
      const postParams = {
        productId: this.productId,
        shopId: id,
        type
      }
      this.isLoading = true
      spuBindUpdateUrl(postParams)
        .then(() => {
          this.$messageSend.success('操作成功')
        })
        .finally(() => {
          this.isLoading = false
          this.query()
        })
        .catch(() => {
          this.isLoading = false
        })
    },
    // 提交商家资质审核
    syncSJZZ(id) {
      const _this = this
      this.$confirm({
        title: '提交商家资质审核',
        content: '你确定要提交商家资质审核吗？',
        okText: '确定',
        okType: 'primary',
        onOk: () => {
          // 发送请求
          return syncMerchantQualificationReview({ poiIdList: [{ supplier_ext_id: id }] })
            .then((rs) => {
              _this.$confirm({
                content: rs,
                onOk() {
                  _this.query()
                },
                onCancel() {}
              })
            })
            .catch((err) => err)
        },
        onCancel() {}
      })
    },
    // 拒绝
    refuseDeal(id) {
      this.rejectVisible = true
      this.rejectId = id
    },
    // 拒绝关闭
    handleRejectCancel() {
      this.rejectId = ''
      this.rejectInfo = ''
    },
    handleReject() {
      if (!this.rejectInfo) {
        this.$messageSend.warning('请填写拒绝原因')
      } else {
        rejectAudit({ id: this.rejectId, materialDesc: this.rejectInfo })
          .then((res) => {
            this.$messageSend.success(res)
            this.rejectInfo = ''
            this.rejectId = ''
            this.rejectVisible = false
            this.query()
          })
          .catch((err) => err)
      }
    },
    // 批量提交资质审核
    batchSubmission() {
      const _this = this
      if (!this.selectedRowKeys.length) {
        this.$messageSend.warning('请选择后再进行操作！')
      } else {
        console.log(_this.selectedRowKeys)
        this.$confirm({
          content: '确定批量提交资质审核吗？',
          onOk() {
            const arr = _this.selectedRowKeys.map((item) => {
              return { supplier_ext_id: item }
            })
            // 发送批量操作请求并刷新
            return syncMerchantQualificationReview({ poiIdList: arr })
              .then((res) => {
                _this.$confirm({
                  content: res,
                  onOk() {
                    _this.query()
                    _this.selectedRowKeys = []
                  },
                  onCancel() {}
                })
              })
              .catch((err) => err)
          },
          onCancel() {}
        })
      }
    },
    batchDelete() {
      const { selectedRowKeys, productId } = this
      if (!this.selectedRowKeys.length) {
        this.$messageSend.warning('请选择后再进行操作！')
      } else {
        this.$confirm({
          content: '确定批量删除吗？',
          onOk: () => {
            const postParams = {
              shopIds: selectedRowKeys,
              productId
            }
            // 发送批量操作请求并刷新
            bindingDelete(postParams)
              .then((res) => {
                this.query()
                this.selectedRowKeys = []
              })
              .finally(() => {
                this.query()
                this.selectedRowKeys = []
              })
              .catch((err) => err)
          },
          onCancel() {}
        })
      }
    }
  },

  mounted() {
    const { id = '', idJudge = '' } = this.$route.query
    this.productId = id
    this.search.bizProductId = id
    this.judgeId = idJudge
    this.setSearch()
    this.query()
  },

  filters: {
    // 获取直播平台
    getPlatform(val) {
      if (!val) return '无'
      return val === 1 ? '抖音' : '快手'
    }
  }
}
</script>

<style lang="less" scoped>
.anchor-review {
  padding: 16px;
}

// 主播信息弹窗
.anchor-info {
  /deep/ .ant-modal-footer {
    border-top: none;
    padding-bottom: 36px;
    text-align: center;
  }

  //  描述列表样式覆盖
  /deep/ .ant-descriptions-view {
    .ant-descriptions-item-label {
      color: #666666;
    }

    .ant-descriptions-item-content {
      color: #333;
      vertical-align: text-top;
    }
  }
}
.color-f55 {
  color: #f55f4e !important;
}
.search-box {
  display: flex;
  justify-content: flex-start;
  width: 100%;
  flex-wrap: wrap;
  > div {
    margin-right: 15px;
    margin-bottom: 5px;
  }
}
.zzimg {
  /deep/.ant-upload-list-item-list-type-picture-card,
  /deep/.ant-upload-list-picture-card-container {
    width: 30px;
    height: 30px;
    margin: -6px 3px 6px 0;
    padding: 3px;
    border-radius: 2px;
  }
}
p {
  margin-bottom: 5px;
  font-size: 13px;
}
.font-weight-bold {
  font-weight: bold;
  display: inline-block;
}

table tbody td div {
  margin-top: 2px;
}
/deep/ table tbody td a {
  margin-right: 0;
}
</style>
